var $ = null, lui = null, table = null;
var pid, tid, classifyId, classifyModel;

layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'util', 'dropdown'], function () {
        lui = layui;
        var form = layui.form,
            util = layui.util;
        $ = layui.$;

        table = layui.table;

        pid = $("#projectList").val();
        openTaskData(table, pid);


        form.render();
        form.on("select(projectList)", function (data) {
            pid = data.value;
            openTaskData(table, pid);
        });

        table.on("row(taskTable)", function (obj) {
            var data = obj.data;
            obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
            tid = data.id;
            classifyModel = data.fillingModel;
            classifyId = data.archivesClassifyId;
            getImagesImport();
        });

        function getImagesImport(data) {
            let where = {pid: pid, tid: tid};
            if (data !== undefined) {
                where = Object.assign({}, where, data);
            }
            table.render({
                elem: "#dataTable",
                url: "./getImagesImportData",
                contentType: "application/json",
                method: "post",
                where: where,
                cellMinWidth: 80,
                page: true,
                limit: 50,
                height: "full-65",
                loading: true,
                size: "sm",
                cols: [
                    [
                        {field: "status", title: "状态", width: 100, templet: "#statusTpl"},
                        {field: "batchCode", title: "批次号", width: 180},
                        {field: "model", title: "文件类别", width: 120, templet: "#fillingModelTpl"},
                        {field: "filename", title: "挂接目录"},
                        {field: "uploadTime", title: "创建时间", width: 140},
                        {field: "uploader", title: "创建人", width: 80},
                        {field: "executionTime", title: "执行时间", width: 140},
                        {field: "executor", title: "执行人", width: 80},
                        {field: "info", title: "信息", width: 300},
                        {fixed: "right", title: "操作", width: 60, toolbar: "#operationBar"},
                    ],
                ]
            });
        }

        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    top.layer.confirm('确定要删除当前选择需要导入的信息吗？ ', function (index) {
                        $.ajax({
                            url: './delArchivesImport', //url
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: {"id": data.id},
                            success: function (result) {
                                if (result.code === 0) {
                                    obj.del();
                                    layer.close(index);
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    OnPost = true;
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                    });
                    break;
                case 'execution':
                    $.ajax({
                        url: './ImagesImportExecute', //url
                        contentType: "application/JSON",
                        dataType: 'json', //数据格式
                        type: 'POST', //请求方式
                        async: false, //是否异步请求
                        data: JSON.stringify({"id": data.id, fillingModel: data.classifyModel}),
                        success: function (result) {
                            if (result.code === 0) {
                                parent.layer.msg(result.message, {icon: 1});
                                table.reload('dataTable', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });

                            } else {
                                OnPost = true;
                                parent.layer.alert(result.message, {icon: 2});
                            }
                        },
                    });
                    break;
                case 'detail':
                    top.layer.open({
                        type: 2,
                        title: '数据导入日志',
                        content: './import/archivesImportLog?pid=' + data.id,
                        area: ['800px', '700px'],
                        shadeClose: false,
                        resize: false,

                    });
                    break;
            }
        });

        util.on("lay-event", {
            newImportTask: function () {
                top.layer.open({
                    type: 2,
                    title: '新建导入任务',
                    content: './import/setImagesImportData?id=&pid=' + pid +
                        "&tid=" + tid +
                        "&classifyId=" + classifyId +
                        "&classifyModel=" + classifyModel,
                    area: ['700px', '400px'],
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        let iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Submit()) {
                            table.reload('dataTable', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            });
                        }
                    },
                    btn2: function (index) {
                        layer.close(index);
                    },
                });
            }
        });


    });


